package com.takeeasy.platform.util;

import java.util.HashMap;
import java.util.Map;

import com.takeeasy.platform.cache.SystemDataCache;

public class URLUtil {

    public static final String ACTION = "action";
    public static final String SUBMIT_TYPE = "submitType";
    public static final String ACTION_NAME = "actionName";
    public static final String ACTION_METHOD_NAME = "methodName";
    public static final String DOMAIN_NAME = "domainName";

    public static final String SUBMIT_TYPE_FORM = "form";
    public static final String SUBMIT_TYPE_AJAX = "ajax";
    public static final String SUBMIT_TYPE_DOWNLOAD = "download";
    public static final String SUBMIT_TYPE_UPLOAD = "upload";
    public static final String SUBMIT_TYPE_FORWARD = "forward";
    public static final String SUBMIT_TYPE_REDIRECT = "redirect";

    public static String URLParameterAdd(String oriUrl, String pKey, String pValue) {
        int in = oriUrl.indexOf("?");
        StringBuilder sb = new StringBuilder(oriUrl);
        if (in == -1) {
            sb.append("?");
        } else {
            sb.append("&");
        }
        sb.append(pKey);
        sb.append("=");
        sb.append(pValue);
        return sb.toString();
    }

    /**
     * 处理提交的uri <br>
     * uri组成：/Action_submiType!ActionName_MethodName@DomainName
     *
     * @param uriFull URL的全地址
     * @return {@link Map} uri解析后的map集合
     */
    public static Map<String, String> GET_ACTION_INFO(String uriFull) {
        String uri = uriFull.substring(0, uriFull.indexOf(".go"));
        Map<String, String> actionConfig = new HashMap<>();
        String[] uriSplite = uri.split("!");
        String[] actionDesc = uriSplite[0].split("_");
        actionConfig.put(SUBMIT_TYPE, actionDesc.length > 1 ? actionDesc[1] : SUBMIT_TYPE_AJAX);
        String[] action = uriSplite[1].split("@");
        String[] actionEntity = action[0].split("_");
        actionConfig.put(ACTION_NAME, actionEntity[0]);
        actionConfig.put(ACTION_METHOD_NAME, actionEntity[1]);
        actionConfig.put(DOMAIN_NAME, action.length > 1 ? action[1] : SystemDataCache.DEFAULT_ACTION_DOMAIN);
        return actionConfig;
    }
}
